Box–Muller Transform
   HOME

TheInfoList



OR:

The Box–Muller transform, by George Edward Pelham Box and
Mervin Edgar Muller Mervin Edgar Muller (1 June 1928 – 3 December 2018) was an American computer scientist and mathematician. The Box–Muller transform is named after him. Biography Muller was born in Hollywood, Los Angeles, on 1 June 1928, as one of four sons t ...
, is a random number sampling method for generating pairs of
independent Independent or Independents may refer to: Arts, entertainment, and media Artist groups * Independents (artist group), a group of modernist painters based in the New Hope, Pennsylvania, area of the United States during the early 1930s * Independ ...
, standard, normally distributed (zero expectation, unit
variance In probability theory and statistics, variance is the expectation of the squared deviation of a random variable from its population mean or sample mean. Variance is a measure of dispersion, meaning it is a measure of how far a set of numbers ...
) random numbers, given a source of uniformly distributed random numbers. The method was in fact first mentioned explicitly by Raymond E. A. C. Paley and
Norbert Wiener Norbert Wiener (November 26, 1894 – March 18, 1964) was an American mathematician and philosopher. He was a professor of mathematics at the Massachusetts Institute of Technology (MIT). A child prodigy, Wiener later became an early researcher i ...
in 1934. The Box–Muller transform is commonly expressed in two forms. The basic form as given by Box and Muller takes two samples from the uniform distribution on the interval , 1/nowiki> and maps them to two standard, normally distributed samples. The polar form takes two samples from a different interval, ˆ’1, +1 and maps them to two normally distributed samples without the use of sine or cosine functions. The Box–Muller transform was developed as a more computationally efficient alternative to the
inverse transform sampling method Inverse transform sampling (also known as inversion sampling, the inverse probability integral transform, the inverse transformation method, Nikolai Smirnov (mathematician), Smirnov transform, or the golden ruleAalto University, N. Hyvönen, Comput ...
. The
ziggurat algorithm The ziggurat algorithm is an algorithm for pseudo-random number sampling. Belonging to the class of rejection sampling algorithms, it relies on an underlying source of uniformly-distributed random numbers, typically from a pseudo-random number gen ...
gives a more efficient method for scalar processors (e.g. old CPUs), while the Box–Muller transform is superior for processors with vector units (e.g. GPUs or modern CPUs).


Basic form

Suppose ''U''1 and ''U''2 are independent samples chosen from the uniform distribution on the
unit interval In mathematics, the unit interval is the closed interval , that is, the set of all real numbers that are greater than or equal to 0 and less than or equal to 1. It is often denoted ' (capital letter ). In addition to its role in real analysis, ...
(0, 1). Let :Z_0 = R \cos(\Theta) =\sqrt \cos(2 \pi U_2)\, and :Z_1 = R \sin(\Theta) = \sqrt \sin(2 \pi U_2).\, Then ''Z''0 and ''Z''1 are
independent Independent or Independents may refer to: Arts, entertainment, and media Artist groups * Independents (artist group), a group of modernist painters based in the New Hope, Pennsylvania, area of the United States during the early 1930s * Independ ...
random variables with a standard normal distribution. The derivation is based on a property of a two-dimensional Cartesian system, where X and Y coordinates are described by two independent and normally distributed random variables, the random variables for ''R''2 and Θ (shown above) in the corresponding polar coordinates are also independent and can be expressed as :R^2 = -2\cdot\ln U_1\, and :\Theta = 2\pi U_2. \, Because ''R''2 is the square of the norm of the standard
bivariate normal In probability theory and statistics, the multivariate normal distribution, multivariate Gaussian distribution, or joint normal distribution is a generalization of the one-dimensional (univariate) normal distribution to higher dimensions. One ...
variable (''X'', ''Y''), it has the
chi-squared distribution In probability theory and statistics, the chi-squared distribution (also chi-square or \chi^2-distribution) with k degrees of freedom is the distribution of a sum of the squares of k independent standard normal random variables. The chi-squa ...
with two degrees of freedom. In the special case of two degrees of freedom, the chi-squared distribution coincides with the
exponential distribution In probability theory and statistics, the exponential distribution is the probability distribution of the time between events in a Poisson point process, i.e., a process in which events occur continuously and independently at a constant average ...
, and the equation for ''R''2 above is a simple way of generating the required exponential variate.


Polar form

The polar form was first proposed by J. Bell and then modified by R. Knop. While several different versions of the polar method have been described, the version of R. Knop will be described here because it is the most widely used, in part due to its inclusion in ''
Numerical Recipes ''Numerical Recipes'' is the generic title of a series of books on algorithms and numerical analysis by William H. Press, Saul A. Teukolsky, William T. Vetterling and Brian P. Flannery. In various editions, the books have been in print since 1 ...
''. Given ''u'' and ''v'', independent and uniformly distributed in the closed interval ˆ’1, +1 set . If or , discard ''u'' and ''v'', and try another pair (''u'', ''v''). Because ''u'' and ''v'' are uniformly distributed and because only points within the unit circle have been admitted, the values of ''s'' will be uniformly distributed in the open interval (0, 1), too. The latter can be seen by calculating the cumulative distribution function for ''s'' in the interval (0, 1). This is the area of a circle with radius \scriptstyle \sqrt, divided by \scriptstyle\pi. From this we find the probability density function to have the constant value 1 on the interval (0, 1). Equally so, the angle θ divided by \scriptstyle 2 \pi is uniformly distributed in the interval [0, 1) and independent of ''s''. We now identify the value of ''s'' with that of ''U''1 and \scriptstyle \theta/(2 \pi) with that of ''U''2 in the basic form. As shown in the figure, the values of \scriptstyle \cos \theta = \cos 2 \pi U_2 and \scriptstyle \sin \theta = \sin 2 \pi U_2 in the basic form can be replaced with the ratios \scriptstyle\cos \theta = u/R = u/\sqrt and \scriptstyle\sin \theta = v/R = v/\sqrt, respectively. The advantage is that calculating the trigonometric functions directly can be avoided. This is helpful when trigonometric functions are more expensive to compute than the single division that replaces each one. Just as the basic form produces two standard normal deviates, so does this alternate calculation. :z_0 = \sqrt \cos(2 \pi U_2) = \sqrt \left(\frac\right) = u \cdot \sqrt and :z_1 = \sqrt \sin(2 \pi U_2) = \sqrt\left( \frac\right) = v \cdot \sqrt.


Contrasting the two forms

The polar method differs from the basic method in that it is a type of rejection sampling. It discards some generated random numbers, but can be faster than the basic method because it is simpler to compute (provided that the random number generator is relatively fast) and is more numerically robust. tp://ftp.taygeta.com/pub/publications/randnum.tar.Z Everett F. Carter, Jr., ''The Generation and Application of Random Numbers'', Forth Dimensions (1994), Vol. 16, No. 1 & 2./ref> Avoiding the use of expensive trigonometric functions improves speed over the basic form. It discards of the total input uniformly distributed random number pairs generated, i.e. discards uniformly distributed random number pairs per
Gaussian Carl Friedrich Gauss (1777–1855) is the eponym of all of the topics listed below. There are over 100 topics all named after this German mathematician and scientist, all in the fields of mathematics, physics, and astronomy. The English eponymo ...
random number pair generated, requiring input random numbers per output random number. The basic form requires two multiplications, 1/2 logarithm, 1/2 square root, and one trigonometric function for each normal variate.Note that the evaluation of 2''U''1 is counted as one multiplication because the value of 2 can be computed in advance and used repeatedly. On some processors, the cosine and sine of the same argument can be calculated in parallel using a single instruction. Notably for Intel-based machines, one can use the fsincos assembler instruction or the expi instruction (usually available from C as an
intrinsic function In computer software, in compiler theory, an intrinsic function (or built-in function) is a function (subroutine) available for use in a given programming language whose implementation is handled specially by the compiler. Typically, it may subst ...
), to calculate complex : \exp(iz) = e^ = \cos z + i \sin z, \, and just separate the real and imaginary parts. Note: To explicitly calculate the complex-polar form use the following substitutions in the general form, Let r = \sqrt and z = 2 \pi u_2. Then : re^ = \sqrt e^ =\sqrt\left \cos(2 \pi u_2) + i \sin(2 \pi u_2)\right The polar form requires 3/2 multiplications, 1/2 logarithm, 1/2 square root, and 1/2 division for each normal variate. The effect is to replace one multiplication and one trigonometric function with a single division and a conditional loop.


Tails truncation

When a computer is used to produce a uniform random variable it will inevitably have some inaccuracies because there is a lower bound on how close numbers can be to 0. If the generator uses 32 bits per output value, the smallest non-zero number that can be generated is 2^. When U_1 and U_2 are equal to this the Box–Muller transform produces a normal random deviate equal to \delta=\sqrt \cos(2 \pi 2^)\approx 6.660. This means that the algorithm will not produce random variables more than 6.660 standard deviations from the mean. This corresponds to a proportion of 2(1-\Phi(\delta)) \simeq 2.738 \times 10^ lost due to the truncation, where \Phi(\delta) is the standard cumulative normal distribution. With 64 bits the limit is pushed to \delta=9.419 standard deviations, for which 2(1-\Phi(\delta)) < 5 \times 10^.


Implementation

The standard Box–Muller transform generates values from the standard normal distribution (''i.e.''
standard normal deviate A standard normal deviate is a normally distributed deviate. It is a realization of a standard normal random variable, defined as a random variable with expected value 0 and variance 1.Dodge, Y. (2003) The Oxford Dictionary of Statis ...
s) with mean ''0'' and standard deviation ''1''. The implementation below in standard
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
generates values from any normal distribution with mean \mu and variance \sigma^2. If Z is a standard normal deviate, then X = Z\sigma + \mu will have a normal distribution with mean \mu and standard deviation \sigma. Note that the random number generator has been seeded to ensure that new, pseudo-random values will be returned from sequential calls to the generateGaussianNoise function. #include #include #include #include std::pair generateGaussianNoise(double mu, double sigma)


See also

*
Inverse transform sampling Inverse transform sampling (also known as inversion sampling, the inverse probability integral transform, the inverse transformation method, Smirnov transform, or the golden ruleAalto University, N. Hyvönen, Computational methods in inverse probl ...
*
Marsaglia polar method The Marsaglia polar method is a pseudo-random number sampling method for generating a pair of independent standard normal random variables. Standard normal random variables are frequently used in computer science, computational statistics, and in ...
, similar transform to Box–Muller, which uses Cartesian coordinates, instead of polar coordinates


References

*


External links

*
How to Convert a Uniform Distribution to a Gaussian Distribution (C Code)
{{DEFAULTSORT:Box-Muller Transform Transforms Non-uniform random numbers Articles with example C++ code